﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using NFL.Core;
using HtmlAgilityPack;

namespace NFL.Business
{
    public static class FinalGames
    {
        public static IList<string> GetFinalGames()
        {
            try
            {
                List<string> completedGames = new List<string>();

                // Create a request for the URL. 
                WebRequest request = WebRequest.Create(ConfigurationSettings.FinalGamesURL);
                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;
                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                //Console.WriteLine(responseFromServer);
                // Clean up the streams and the response.
                reader.Close();
                response.Close();

                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(responseFromServer);
                var finalGames = doc.DocumentNode.Descendants("a").Where(d => d.Attributes.Contains("title") && d.Attributes["title"].Value.StartsWith("Final"));

                foreach (var game in finalGames)
                {
                    completedGames.Add(game.InnerText.Replace("vs.", string.Empty).Replace(" ", string.Empty));
                }
          
                return completedGames;
            }
            catch
            {
                return null;
            }
        }
    }
}
